//
//  PhotoTableViewController.swift
//  健身宝
//
//  Created by hzt on 2017/10/17.
//  Copyright © 2017年 hzt. All rights reserved.
//

import UIKit

class PhotoTableViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
    var collectionView:UICollectionView!
    var photolist:NSMutableArray?
    lazy var refresh = UIRefreshControl()
    //var list:NSArray?
    override func viewDidLoad() {
        super.viewDidLoad()
        
        viewConfig()
        
        DispatchQueue.global(qos: .default).async {
            self.getPhotoList()
        }
        
    }
    
    func viewConfig(){
        view.backgroundColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1)
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: UIScreen.main.bounds.width/2, height: UIScreen.main.bounds.width/2)
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 0
        
        collectionView = UICollectionView(frame: CGRect(x:0,y:0,width:UIScreen.main.bounds.width,height:UIScreen.main.bounds.height-69-44), collectionViewLayout: flowLayout)
        collectionView.register(CustomCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        collectionView.backgroundColor = .white
        collectionView.delegate = self
        collectionView.dataSource = self
        view.addSubview(collectionView)
        
        refresh.addTarget(self, action: #selector(refreshData), for: .valueChanged)
        refresh.attributedTitle = NSAttributedString(string: "正在刷新")
        collectionView.addSubview(refresh)
        
        let addBtn = UIButton(frame: CGRect(x: 0, y: UIScreen.main.bounds.height-100, width: 44, height: 44))
       // addBtn.setImage(UIImage(named:"add")?.scaleImageToSize(CGSize(width: 38, height: 38)), for: .normal)
        addBtn.setImage(UIImage(named:"add"), for: .normal)
        addBtn.center.x = UIScreen.main.bounds.width/2
        view.addSubview(addBtn)
        
        addBtn.addTarget(self, action: #selector(add), for: .touchUpInside)
    }
    
    func getPhotoList(){
        let urlStr = RO_GetPhotoList
        let paraDict = ["sessionid":SessionID,"mod":"shaitu","related":"1"]
        AFHTTPRequestOperationManager().post(urlStr, parameters: paraDict, success: { (response:AFHTTPRequestOperation?, data:Any?) in
            let array = data as! NSArray
            //print(array)
            self.photolist = array.mutableCopy() as! NSMutableArray
            //print(self.clubList)
            DispatchQueue.main.async {
            self.collectionView.reloadData()
            }
        }) { (response:AFHTTPRequestOperation?, error:Error?) in
            print(response!.debugDescription)
        }
        
    }
    
//    func getList(){
//        let parameter:NSDictionary = ["SessionID":LoginModel.sessionID,"mod":"shaitu","related":"1"]
//        Networking.shared.getSharingList(parameter: parameter) { (json) in
//            self.list = json
//            DispatchQueue.main.async {
//                self.collectionView.reloadData()
//            }
//        }
//    }
    
    @objc func refreshData(){
        getPhotoList()
        refresh.endRefreshing()
    }
    
    @objc func add(){
        hidesBottomBarWhenPushed = true
        _ = navigationController?.pushViewController(PhotoAddController(), animated: true)
        hidesBottomBarWhenPushed = false
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return photolist?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCollectionViewCell
        let dict = photolist![indexPath.row] as! NSDictionary
        
        cell.label.text = dict.value(forKey: "title") as? String
        let pURL = "http://172.24.10.175/workout/Uploads/"
        cell.imageView?.sd_setImage(with: URL(string: pURL+(dict.value(forKey: "thumb0") as! String)))
        
        return cell
    }
    
//    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//        let dict = photolist![indexPath.row] as! NSDictionary
//        //print(dict)
//        let commentArray = dict.value(forKey: "comment") as! NSArray
//        let controller = SharingDetailController()
//        controller.infoArray = commentArray.mutableCopy() as! NSMutableArray
//        controller.userID = dict.value(forKey: "userid") as! String
//        controller.shaituID = dict.value(forKey: "id") as! String
//        //print(controller.userID)
//        print(photolist)
//        //hidesBottomBarWhenPushed = true
//        _ = navigationController?.pushViewController(controller, animated: true)
//        //hidesBottomBarWhenPushed = false
//    }

    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let controller = PhotoDetailViewController()
        controller.infoDict = photolist![indexPath.row] as! NSDictionary
        _ = navigationController?.pushViewController(controller, animated: true)
    }
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
}

